package top.hongthorn.util;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.DmTypeConvert;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyBastsPlusUtil {

    //数据库url
    private static final String url = "jdbc:mysql://localhost:3306/aircraftcarriersystem?serverTimezone=UTC";
    //数据库账号
    private static final String username = "root";
    //密码
    private static final String password = "250056";

    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        //多表同时生成
        tables.add("helicopter");
        DataSourceConfig.Builder builder1 = new DataSourceConfig.Builder(url, username, password);
        builder1.typeConvert(new DmTypeConvert());
        //模式名
        builder1.schema("top.hongthorn");

        FastAutoGenerator.create(builder1)
                .globalConfig(builder -> {
                    builder.author("泓宇网络科技工作室")               //作者
                            .outputDir(System.getProperty("user.dir") + "/src/main/java")    //输出路径(写到java目录)
                            .enableSwagger()           //开启swagger
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .fileOverride();            //开启覆盖之前生成的文件

                })
                .packageConfig(builder -> {
                    builder.parent("top.hongthorn")
                            .entity("pojo")
                            .service("service")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper")); // 设置mapperXml生成路径

                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("hongyu_")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .logicDeleteColumnName("deleted")
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .enableBaseResultMap()  //生成通用的resultMap
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}